package se581.banker.event;

import se581.banker.BankerConstants;
import se581.banker.BankerConstants.TrackerEventGroup;
import se581.banker.BankerConstants.TrackerEventType;

/**
 * These are the AccountTracker System events - update, new, query whereis, etc.
 * 
 * @author mmichalak
 *
 */
public class TrackerEvent {

	/*
	 * Seq id.  Basically the line number from the input event 
	 * file.
	 */
	int systemId = 0;
	TrackerEventGroup eventGroup;
	String id;
	int tick;
	TrackerEventType eventType;
	int transactionAmt;
	int queryTickTime;
	
	public boolean isQueryEvent() {
		return false;
	}

	public int getTick() {
		return tick;
	}

	public void setTick(int tick) {
		this.tick = tick;
	}

	public TrackerEventType getEventType() {
		return eventType;
	}

	public void setEventType(TrackerEventType eventType) {
		this.eventType = eventType;
	}

	public int getTransactionAmt() {
		return transactionAmt;
	}

	public void setTransactionAmt(int xtnAmt) {
		this.transactionAmt = xtnAmt;
	}

	public int getQueryTickTime() {
		return queryTickTime;
	}

	public void setQueryTickTime(int queryTickTime) {
		this.queryTickTime = queryTickTime;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return "Event [systemId=" + systemId + ", id=" + id + ", tick=" + tick + ", eventType="
				+ eventType + ", transactionAmt=" + transactionAmt 
				+ ", queryTickTime=" + queryTickTime + ", failProcessor="
				+ "]";
	}

	public TrackerEventGroup getEventGroup() {
		return eventGroup;
	}

	public void setEventGroup(TrackerEventGroup eventGroup) {
		this.eventGroup = eventGroup;
	}
	
	public String toEventLine() {
		StringBuilder sb = new StringBuilder(20);
		sb.append(tick).append(" ");
		
		if ( eventType != null && 
				(eventType == BankerConstants.TrackerEventType.UPDATE || eventType == BankerConstants.TrackerEventType.NEW) )
			sb.append(eventType).append(" ")
				.append(id).append(" ")
				.append(transactionAmt).append(" ");
		else if ( eventType != null && 
				eventGroup == BankerConstants.TrackerEventGroup.QUERY )
			sb.append(eventGroup).append(" ")
				.append(eventType).append(" ")
				.append(id).append(" ");

		
		return sb.toString();
	}

	public int getSystemId() {
		return systemId;
	}

	public void setSystemId(int systemId) {
		this.systemId = systemId;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((eventGroup == null) ? 0 : eventGroup.hashCode());
		result = prime * result
				+ ((eventType == null) ? 0 : eventType.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + queryTickTime;
		result = prime * result + systemId;
		result = prime * result + tick;
		result = prime * result + transactionAmt;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TrackerEvent other = (TrackerEvent) obj;
		if (eventGroup != other.eventGroup)
			return false;
		if (eventType != other.eventType)
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (queryTickTime != other.queryTickTime)
			return false;
		if (systemId != other.systemId)
			return false;
		if (tick != other.tick)
			return false;
		if (transactionAmt != other.transactionAmt)
			return false;
		return true;
	}
}
